package org.example.library.controller;


import lombok.extern.slf4j.Slf4j;
import org.example.library.entity.BookInfo;
import org.example.library.entity.PageRequest;
import org.example.library.entity.PageResult;
import org.example.library.entity.Result;
import org.example.library.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;
    @RequestMapping("/getList")
    public List<BookInfo> getList()
    {
        List<BookInfo> bookInfos=bookService.getList();
        return bookInfos;
    }
    @RequestMapping("/getListByPage")
    public Result<PageResult> getListByPage(PageRequest pageRequest){
        log.info("pageRequest:{}",pageRequest);
        PageResult pageResult = bookService.getListByPage(pageRequest);
        return Result.success(pageResult);
    }
    @RequestMapping(value = "/addBook", produces = "application/json;charset=utf-8")
    public Result<String> addBook(BookInfo bookInfo){
        log.info("添加图书, bookInfo: {}", bookInfo);
        //1. 参数校验
        //2. 添加图书
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() ==null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() ==null
        ){
            return Result.fail("参数不合法，请重新检查");
        }
        Integer result = bookService.addBook(bookInfo);

        return result==1?Result.success(""):Result.fail("图书插入失败");
    }
    @RequestMapping("/deleteBook")
    public Result<String> deleteBook(Integer bookId) {
        log.info("删除图书, bookId: {}", bookId);
        if (bookId == null || bookId <= 0) {
            return Result.fail("参数不合法");
        }

        Integer result = bookService.deleteBook(bookId);
        if (result == 1) {
            return Result.success("");
        } else {
            return Result.fail("删除失败");
        }
    }
    @RequestMapping("/updateBook")
    public Result<String> updateBook(BookInfo bookInfo) {
        log.info("更新图书, bookInfo: {}", bookInfo);

        if (bookInfo.getId() == null || bookInfo.getId() <= 0) {
            return Result.fail("图书ID不能为空");
        }

        Integer rows = bookService.updateBookById(bookInfo);
        if (rows == 1) {
            return Result.success(""); // 更新成功
        } else {
            return Result.fail("更新失败");
        }
    }
    @RequestMapping("/queryBookById")
    public Result<BookInfo> queryBookById(Integer bookId){
        log.info("查询图书详情, bookId: {}", bookId);
        if (bookId == null || bookId <= 0) {
            return Result.fail("参数不合法");
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        if (bookInfo == null) {
            return Result.fail("图书不存在");
        }
        return Result.success(bookInfo);
    }
    @RequestMapping("/batchDeleteBook")
    public Result<String> batchDeleteBook(@RequestParam List<Integer> ids) {
        log.info("批量删除图书, ids: {}", ids);

        if (ids == null || ids.isEmpty()) {
            return Result.fail("请选择要删除的图书");
        }

        try {
            int count = bookService.batchDeleteBook(ids);
            return Result.success("成功删除 " + count + " 本书");
        } catch (Exception e) {
            log.error("批量删除失败", e);
            return Result.fail("删除失败：" + e.getMessage());
        }
    }
}
